package com.hyt.it.ogt.kq.service.bm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.service.bm.mapper.DictMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.Dict;
import com.hyt.it.ogt.kq.service.bm.service.IDictService;

import cn.hutool.core.collection.CollUtil;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author qiaozj
 * @since 2020-09-11
 */
@Service
@Transactional
public class DictServiceImpl extends BaseServiceImpl<DictMapper, Dict> implements IDictService {
	
	@Resource
    private DictMapper dictMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private ConfigManager configManager;

    @Override
    public List<Dict> getByType(String type) {
    	
    	List<Dict> dictList = new ArrayList<>();
        String dictCacheKey = Dict.getCacheKey(type);
        String dictCacheValue = redisTemplate.opsForValue().get(dictCacheKey);
        if (StringUtils.isNotEmpty(dictCacheValue)) {
        	dictList = FastJsonUtil.getJsonToList(dictCacheValue, Dict.class);
        } else {
            // 查询字典类型下的配置列表
        	dictList = dictMapper.getByType(type);
            if (CollUtil.isNotEmpty(dictList)) {
                String json = FastJsonUtil.getBeanToJson(dictList);
                // 添加数据缓存
                redisTemplate.opsForValue().set(dictCacheKey, json, configManager.getRedisDataInvalidTime(), TimeUnit.SECONDS);
            }
        }
    	return dictList;
    }
}
